package cn.itsource.ymcc.producer;

import cn.itsource.ymcc.constant.MediaConstant;
import cn.itsource.ymcc.domain.MediaFile;
import cn.itsource.ymcc.result.JsonResult;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

@Component
public class MediaProducerImpl implements MediaProducer {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public JsonResult syncSend(MediaFile mediaFile) {
        //设置 主题:标签
        SendResult sendResult = rocketMQTemplate.syncSend(
                MediaConstant.TOPIC_COURSE_VIDEO_PUSH_TO_SRS + ":" + MediaConstant.TAGS_COURSE_VIDEO_PUSH_TO_SRS,
                //我们要传 字符串
                MessageBuilder.withPayload(JSON.toJSONString(mediaFile)).build()
        );
        return sendResult.getSendStatus()== SendStatus.SEND_OK?JsonResult.success():JsonResult.error("发送消息失败");
    }
}
